#ifndef __STORAGE_H_
#define __STORAGE_H_

#include "EEPROM.h"
#include "assert.h"
#include "stdlib.h"

//todo 删掉
#include	"UART.h"
#include "stdio.h"

//整个FLASH空间为17K，地址范围为0x0000-0x4400
//每张图片360字节，假设存储16张图片，所需空间5760字节，假设分配EEPROM大小为6KB，空闲384字节
//设置最后

/*|-------------------------------------------|--------------------|----------|
	|-----------------------------------------13.5KB--------------16.5KB-------17KB
	|------------------10.5KB-------------------|---------3KB--------|---0.5KB--|
	0x00--------------------------------------0x3600--------------0x4200-----0x4400
	|-----------------------------------------0x0000--------------0x0C00-----0x0E00
*/

#define STORAGE_BMP_ADDR 0x0
#define STORAGE_INFO_ADDR 0xC00
#define STORAGE_END_ADDR 0xE00

#define STORAGE_INIT_MAGIC_NUM 0xBEEF

#define INVALID_BMP_UUID 0xFFFF

#define INVALID_BMP_ADDR 0xFFFF

#define BMP_SIZE 360

#define MAX_BMP_NUMBER 8

#define EEPROM_SECTOR_SIZE 512

#define MAX_KEYBOARD_LAYER 8

#define INVALID_KEYBOARD_LAYER 0xFF

#define ERROR_BMP_UUID 0xFFFE

typedef struct
{
	unsigned short uuid; //标识每张图片
	unsigned short addr; //图片存储起始地址
} ST_BmpInfo;			 //4字节

typedef struct
{
	//键盘基本信息
	char version;		  //应用程序内容版本  1字节
	unsigned short magicNum;	//表示EEPROM设备是否已经经过初始化  2字节

	//图片相关信息
	char bmpNumber; //共有多少张图片  1字节
	
	ST_BmpInfo pics[MAX_BMP_NUMBER];		//存储所有图片的信息  4x8=32字节

	unsigned short bmpDisplayList[MAX_BMP_NUMBER];//显示顺序，第几层显示第几个 2x8=16字节

} ST_DevInfo;

//当前显示的图片UUID
extern unsigned short g_currentBmpUUID;
//上电后就从EEPROM读取设备信息到这里
extern ST_DevInfo g_devInfo;

extern char buff[360];

int readStorageInfo(ST_DevInfo *info);

int writeStorageInfo(ST_DevInfo *info);
//获取当前uuid对应的图片数据
int readBmpByUUID(unsigned short uuid, unsigned char *buff[BMP_SIZE]);

int writeBmpData(unsigned short addr, unsigned char *buff[BMP_SIZE]);

int addBmp(unsigned short uuid, unsigned char *buff[BMP_SIZE]);

unsigned short getUUIDByLayer(char layer);

//获取当前层显示的图片数据
int readBmpByLayer(char layer, unsigned char *buff[BMP_SIZE]);

//获取已经存在的uuid对应的地址
unsigned short getExistUUIDAddr(unsigned short uuid);

unsigned short getNewUUIDAddr(unsigned short uuid);

int isAddrUsed(unsigned short addr);

int getBmpIndexByUUID(unsigned short uuid);

int getUnusedIndex();

//todo 删除一张图片

#endif